﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
using Baosight.ZNH.ZMCA.UI.Controls;
using Microsoft.Practices.Unity;
using Baosight.ZNH.ZMCA.UI.Utils;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCA.UI.PopWindows;
using Baosight.ZNH.ZMCA.UI.Model;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCA.UI.Utils;

namespace Baosight.ZNH.ZMCA.UI
{
    public partial class ZMCA43 : EFViewCmes
    {
        private const int fontSizeNumber = 14;
       
       
        EIBlock blockUser = null;
        EIBlock blockClass = null;
        /// <summary>
        /// 生产重点关注：工作指示
        /// 供00S00Portal.xmal页面调用
        /// </summary>
        public ZMCA43()
        {
            InitializeComponent();
            this.Loaded +=new RoutedEventHandler(ZMCA43_Loaded);
        }

        private void ZMCA43_Loaded(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA50");
            eiInfo.Set("method", "initQuery");

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, onInitLoadCompleted);
        }

        private void onInitLoadCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetStatus() == -1)
            {
                EFWindow.Alert("数据加载失败！");
                return;
            }
            else
            {
                //btnMoreIndications.FontSize = fontSizeNumber;
                //现实前5条记录
                blockUser = e.EIInfo.GetBlock("cmes.cp.people");
                blockClass = e.EIInfo.GetBlock("ProblemType");
                EIBlock block = e.EIInfo.GetBlock("result");
                if (block != null && block.GetRowCount() >= 5)
                {
                    for (int i = block.GetRows().Count; i > 4; i--)
                    {
                        EIRow row = block.GetRow(i);
                        block.GetRows().Remove(row);
                    }
                }

                DateTime nowTime = new DateTime();
                nowTime = System.DateTime.Now;
                string time = nowTime.ToString("yyyy-MM-dd");
                //    <StackPanel Name="mainAttention1" Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  Grid.Row="0" Grid.Column="0">
                //    <HyperlinkButton x:Name="btnAttention1" Background="Transparent" Click="HyperlinkButton_Click"></HyperlinkButton>
                //    <TextBlock x:Name="tbAttention1" Text="今日新增" Foreground="Red"></TextBlock>
                //</StackPanel>



                for (int i = 1; i <= block.GetRows().Count; i++)
                {
                    EIRow row = block.GetRow(i-1);
                    string date = row["fdCheckTime"].ToString();
                    string desc = row["fdCheckContent"].ToString();
                    
                    StackPanel panel = new StackPanel();
                    panel.Name = "mainAttention" + i;
                    panel.Orientation = Orientation.Horizontal;
                    panel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                    panel.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;     
                    panel.SetValue(Grid.RowProperty, i);
                    panel.SetValue(Grid.ColumnProperty, 0);

                    HyperlinkButton hbutton = new HyperlinkButton();
                    hbutton.Name = "btnWorkIndication" + i;
                    hbutton.Background = new SolidColorBrush(Colors.Transparent);
                    if(row["fdStatus"].ToString() == "已反馈")
                        hbutton.Foreground = new SolidColorBrush(Colors.Green);
                    else
                        hbutton.Foreground = new SolidColorBrush(Colors.Black);
                    hbutton.FontSize = fontSizeNumber;
                    hbutton.Margin = new Thickness(15, 0, 0, 0);
                    hbutton.DataContext = row;
                    hbutton.Content = InterceptStrConverter.GetAttetionDesc(date, ref desc);
                    hbutton.Click += new RoutedEventHandler(HyperlinkButton_Click);

                    TextBlock tblock = new TextBlock();
                    tblock.Name = "tbWordIndication" + i;
                    tblock.Text = "今日新增";
                    tblock.Foreground = new SolidColorBrush(Colors.Red);
                    tblock.Visibility = System.Windows.Visibility.Collapsed;

                    panel.Children.Add(hbutton);
                    panel.Children.Add(tblock);

                    if (date == time)
                    {
                        tblock.Visibility = System.Windows.Visibility.Visible;
                    }

                    this.LayoutRoot.Children.Add(panel);
                }
            }
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton btn = sender as HyperlinkButton;
            EIRow row = btn.DataContext as EIRow;
            //string problemId = (btn.Parent.FindChildByType<TextBlock>().FindName("problemId") as TextBlock).Text.ToString();
            //string type = (btn.Parent.FindChildByType<TextBlock>().FindName("type") as TextBlock).Text.ToString();
          //  string problemId = row["fdProblemId"].ToString();
          //  string type = row["fdProblemTypeId"].ToString(); ;
            string keyId = row["fdKeyId"].ToString();
            Instruction selInstruction = new Instruction();
            selInstruction.FdCheckContent = Common.GetStringValue(row["fdCheckContent"]);
            selInstruction.FdFeedbackPersonId = Common.GetStringValue(row["fdFeedbackPersonId"]);
            selInstruction.FdCheckpersonId = Common.GetStringValue(row["fdCheckpersonId"]);
            selInstruction.FdCheckTime = Common.GetStringValue(row["fdCheckTime"]);          
            selInstruction.FdKeyId = Common.GetStringValue(row["fdKeyId"]);
            selInstruction.FdProblemId = Common.GetStringValue(row["fdProblemId"]);
            selInstruction.FdProblemTypeId = Common.GetStringValue(row["fdProblemTypeId"]);
            selInstruction.FdStatus = Common.GetStringValue(row["fdStatus"]);
            selInstruction.FdFeedbackTime = Common.GetStringValue(row["fdFeedbackTime"]);
            selInstruction.FdFeedbackContent = Common.GetStringValue(row["fdFeedbackContent"]);
         //   ZMCASpecialCommon.ShowPopwindows("4", keyId, content_DataChanged, true);         
            ZMCA45 content45 = new ZMCA45();
            content45.blockClass = blockClass;
            content45.blockUser = blockUser;
            CmesWindows window = new CmesWindows();
            window.Header = "查看";
            content45.SetInstructionValue(selInstruction);
           
            window.Content = content45;
            window.ShowDialog();              
            
        }

       
        void content_DataChanged(object sender, EventArgs e)
        {
            UpdateHomePageData(new EIInfo());
        }

        ///<summary>
        ///更新主表
        ///</summary>
        ///<param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }

        private void btnMoreIndications_Click(object sender, RoutedEventArgs e)
        {
            IShell shell = this.Container.Resolve<IShell>();
            Dictionary<string, string> paraData = new Dictionary<string, string>();
            shell.LoadView("ZMCA50", paraData);
        }

        public Orientation Horizontal { get; set; }
    }

    public class InterceptStrConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string date = Common.GetStringValue(value.GetType().GetProperty("fdCheckTime").GetValue(value, null)) + " ";
            string desc = Common.GetStringValue(value.GetType().GetProperty("fdCheckContent").GetValue(value, null));

            return GetAttetionDesc(date, ref desc);
        }

        public static string GetAttetionDesc(string date ,ref string desc)
        {
            if (desc != null && desc.ToString().Length > 14)
            {
                desc = desc.ToString().Replace("\r", "");
                return date + " " + desc.ToString().Substring(0, 14) + "...";
            }
            else
            {
                return date + " " + desc;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
